#include "tim_config.h"

void my_tim_init(void)
{
#if TIMER_TEST_1
    // 开启时钟
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
    // 初始化TIM3
    TIM_TimeBaseInitTypeDef TIM3_TimeBaseInit;
    TIM3_TimeBaseInit.TIM_Prescaler         = 7200 - 1; // 10KHz
    TIM3_TimeBaseInit.TIM_CounterMode       = TIM_CounterMode_Up;
    TIM3_TimeBaseInit.TIM_Period            = 10000 - 1; // 1s
    TIM3_TimeBaseInit.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM3, &TIM3_TimeBaseInit);
    // 使能TIM3
    TIM_Cmd(TIM3, ENABLE);
    // 配置TIM3中断
    TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
    // 配置TIM3中断优先级
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel                   = TIM3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 2;
    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    // 一路互补PWM输出 驱动
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
    // 配置TIM1
    TIM_TimeBaseInitTypeDef TIM1_TimeBaseInit;
    TIM1_TimeBaseInit.TIM_Prescaler         = 7200 - 1; // 10KHz
    TIM1_TimeBaseInit.TIM_CounterMode       = TIM_CounterMode_Up;
    TIM1_TimeBaseInit.TIM_Period            = 100 - 1; // 10ms
    TIM1_TimeBaseInit.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM1, &TIM1_TimeBaseInit);

    TIM_ARRPreloadConfig(TIM1, ENABLE);

    // 配置输出比较
    TIM_OCInitTypeDef TIM1_OCInit;
    TIM1_OCInit.TIM_OCMode       = TIM_OCMode_PWM1;
    TIM1_OCInit.TIM_OCPolarity   = TIM_OCPolarity_High;
    TIM1_OCInit.TIM_OCNPolarity  = TIM_OCNPolarity_High;
    TIM1_OCInit.TIM_OutputState  = TIM_OutputState_Enable;
    TIM1_OCInit.TIM_OutputNState = TIM_OutputNState_Enable;
    TIM1_OCInit.TIM_Pulse        = 0;
    TIM_CCPreloadControl(TIM1, ENABLE);
    TIM_OC1Init(TIM1, &TIM1_OCInit);
    // 使能TIM1
    TIM_Cmd(TIM1, ENABLE);
    TIM_CtrlPWMOutputs(TIM1, ENABLE);
    TIM_SetCompare1(TIM1, 0);
#endif
    // 配置TIM3 CH1 引脚
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_6;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    // 配置TIM3输出PWM信号
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
    TIM_TimeBaseInitTypeDef TIM3_TimeBaseInit;
    TIM3_TimeBaseInit.TIM_Prescaler         = 720 - 1; // 100KHz
    TIM3_TimeBaseInit.TIM_CounterMode       = TIM_CounterMode_Up;
    TIM3_TimeBaseInit.TIM_Period            = 100 - 1; // 1e-3
    TIM3_TimeBaseInit.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM3, &TIM3_TimeBaseInit);
    TIM_ARRPreloadConfig(TIM3, ENABLE);

    TIM_OCInitTypeDef TIM3_OCInit;
    TIM3_OCInit.TIM_OCMode      = TIM_OCMode_PWM1;
    TIM3_OCInit.TIM_OCPolarity  = TIM_OCPolarity_High;
    TIM3_OCInit.TIM_OutputState = TIM_OutputState_Enable;
    TIM3_OCInit.TIM_Pulse       = 50;
    TIM_CCPreloadControl(TIM3, ENABLE);
    TIM_OC1Init(TIM3, &TIM3_OCInit);

    TIM_Cmd(TIM3, ENABLE);
    TIM_CtrlPWMOutputs(TIM3, ENABLE);
    // 配置TIM1 输入捕获
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_8;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IPD;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    TIM_TimeBaseInitTypeDef TIM1_TimeBaseInit;
    TIM1_TimeBaseInit.TIM_Prescaler         = 72 - 1; // 100KHz
    TIM1_TimeBaseInit.TIM_CounterMode       = TIM_CounterMode_Up;
    TIM1_TimeBaseInit.TIM_Period            = 2 << 15 - 1; // 1e-3
    TIM1_TimeBaseInit.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM1, &TIM1_TimeBaseInit);
    TIM_Cmd(TIM1, ENABLE);
    TIM_ARRPreloadConfig(TIM1, ENABLE);

    TIM_ICInitTypeDef TIM1_ICInit;
    TIM1_ICInit.TIM_Channel     = TIM_Channel_1;
    TIM1_ICInit.TIM_ICFilter    = 0;
    TIM1_ICInit.TIM_ICPolarity  = TIM_ICPolarity_Rising;
    TIM1_ICInit.TIM_ICPrescaler = TIM_ICPSC_DIV1;
    TIM1_ICInit.TIM_ICSelection = TIM_ICSelection_DirectTI;
    TIM_ICInit(TIM1, &TIM1_ICInit);
    TIM1_ICInit.TIM_Channel     = TIM_Channel_2;
    TIM1_ICInit.TIM_ICFilter    = 0;
    TIM1_ICInit.TIM_ICPolarity  = TIM_ICPolarity_Falling;
    TIM1_ICInit.TIM_ICPrescaler = TIM_ICPSC_DIV1;
    TIM1_ICInit.TIM_ICSelection = TIM_ICSelection_IndirectTI;
    TIM_ICInit(TIM1, &TIM1_ICInit);

    TIM_SelectInputTrigger(TIM1, TIM_TS_TI1FP1);
    TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_Reset);
}
void my_systick_init(void)
{
    SysTick_Config(SystemCoreClock / 1000);
}